<?php
class knowledge extends Model {
	var $_table = "xdf_knowledge";
	var $_field = array('id','name','pid','sub_id','create_time','content');
	var $_skill_table = "xdf_skill";
	var $_skill_field = array('id','name','content','pid','sub_id','create_time');
	var $_type_table = "xdf_item_type";
	var $_sub_table = "xdf_subject";
	
	/**
	 * 获取题型
	 *
	 */
	public function gettypeBySubId($sub_id) {
		$sql_row = "SELECT * FROM $this->_type_table WHERE sub_id = $sub_id";
		return $this->fetchAll($sql_row);
	}
	/**
	 * 获取科目
	 *
	 */
	public function getSub() {
		$sql_row = "SELECT * FROM $this->_sub_table";
		return $this->fetchAll($sql_row);
	}
	
	/**
	 * 获取某个教师的所有科目
	 *
	 */
	public function getSubByTeacher($user_id) {
		$sql_row = "SELECT s.id,s.name FROM `xdf_teacher_subject` t LEFT JOIN  $this->_sub_table s ON t.subject_id = s.id WHERE t.teacher_id = $user_id ";
		return $this->fetchAll($sql_row);
	}
	/**
	 * 获取知识点
	 *
	 * @param 科目id $sub_id
	 * @param 父知识点id $p_id
	 *        	默认为0（0为跟节点）
	 */
	public function getknowsBySubId($sub_id, $p_id = 0) {
		$sql_row = "SELECT * FROM $this->_table WHERE pid = $p_id AND sub_id = $sub_id";
		return $this->fetchAll($sql_row);
	}
	
	/**
	 * 获取解题技巧
	 *
	 * @param 科目id $sub_id
	 * @param 父知识点id $p_id
	 *        	默认为0（0为跟节点）
	 */
	public function getskillBySubId($sub_id, $p_id = 0) {
		$sql_row = "SELECT * FROM $this->_skill_table WHERE pid = $p_id AND sub_id = $sub_id";
		return $this->fetchAll($sql_row);
	}
	

	/**
	 * 获取科目列表
	 */
	public function getSubList(){
		$sql = "SELECT * FROM $this->_sub_table";
		return $this->fetchAll($sql);
	}
	
	
	
	/**
 	 * 获取解题技巧/知识点的某个节点的所有子孩子
	 * @param 父知识点id $pid
	 * @param 类型 0-知识点 1-解题技巧
	 */
// 	public function getKnowsChildren($pid,$type = 0){
// 		$str = '';
// 		if($type){
// 			$table = $this->_skill_table;
// 		}else{
// 			$table = $this->_table;
// 		}
// 		$sql = "SELECT id FROM $table WHERE pid = $pid";
// 		$result = $this->fetchAll($sql);
// 		if($result){
// 			$str = join('_', $result);
// 			foreach ($result as $val){
// 				$child_str = $this->getKnowsChildren($val['id'],$type);
// 				if($child_str ){
// 					$str .= $child_str;
// 				}
// 			}
// 			return $str;
// 		}else{
// 			return false;
// 		}
// 	}
	//获取知识点和解题技巧的所有子节点1：解题技巧，0：知识点
	public function getKnowsChildren($pid,$type = 0){
		$knows=array();
		$str = '';
		if($type){
			$table = $this->_skill_table;
		}else{
			$table = $this->_table;
		}
		$sql = "SELECT id FROM $table WHERE pid = $pid";
		$result = $this->fetchAll($sql);
// 		echo "<br>-----------<br>";
// 		print_r($result);
// 		echo "<br>-----------<br>";
		if(is_array($result))
		foreach ($result as $val){
			$knows=array_merge($result,$knows);
// 			echo "<br>";
// 			echo "<br>------pid-----";
// 			echo $val['id'];
// 			echo "-----------<br>";
			$result = $this->getKnowsChildren($val['id'],$type);
			$knows=array_merge($result,$knows);
		}
		return $knows;
	}
	
}